package com.hc.controller;

import com.hc.entity.TDict;
import com.hc.service.IDataDictService;
import com.hc.vo.GetDataDictVO;
import com.hc.vo.QueryDictVO;
import com.hc.vo.StatusVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dict")
public class DataDictController {

    IDataDictService dataDictService;


    @Autowired
    public void setDataDictService(IDataDictService dataDictService) {
        this.dataDictService = dataDictService;
    }

    @GetMapping
    @ApiOperation(value = "根据表名,字段名获得该字段可用所有值和值的描述", produces = MediaType.APPLICATION_JSON_VALUE, response = GetDataDictVO.class, httpMethod = "GET")
    public StatusVO getDictValues(@RequestParam String tableName, @RequestParam String colName) {
        List<TDict> dicts = dataDictService.listByTableNameColName(tableName, colName);
        List<GetDataDictVO> data = dicts.stream().map(tDict -> {
            GetDataDictVO vo = new GetDataDictVO();
            BeanUtils.copyProperties(tDict, vo);
            return vo;
        }).collect(Collectors.toList());
        return new StatusVO(dicts);
    }

    //  pathVar貌似前端难写
    @GetMapping("/{tableName}/{colName}")
    @ApiOperation(value = "根据表名,字段名获得该字段可用所有值和值的描述", produces = MediaType.APPLICATION_JSON_VALUE, response = GetDataDictVO.class, httpMethod = "GET")
    public StatusVO getDictValuesRest(@PathVariable String tableName, @PathVariable String colName) {
        List<TDict> dicts = dataDictService.listByTableNameColName(tableName, colName);
        List<GetDataDictVO> data = dicts.stream().map(tDict -> {
            GetDataDictVO vo = new GetDataDictVO();
            BeanUtils.copyProperties(tDict, vo);
            return vo;
        }).collect(Collectors.toList());
        return new StatusVO(data);
    }



}
